package com.shuigu.controller;

import com.shuigu.response.ShuiGuResult;
import com.shuigu.pojo.dto.UmsRoleParamDto;
import com.shuigu.pojo.entity.UmsRole;
import com.shuigu.service.IUmsRoleService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hsg
 * @since 2024-06-01
 */
@RestController
@RequestMapping("/ums/role")
public class UmsRoleController {

    @Resource
    private IUmsRoleService umsRoleService;

    /**
     * 新增角色接口
     */
    @PostMapping
    public ShuiGuResult addSysRole(@RequestBody UmsRole role) {
        boolean flag = umsRoleService.save(role);
        if(flag) {
            return ShuiGuResult.success();
        }
        return ShuiGuResult.error();
    }

    /**
     * 查询角色列表接口
     */
    @GetMapping
    public ShuiGuResult searchList() {
        List<UmsRole> list = umsRoleService.list();
        list.forEach(System.out::println);
        return ShuiGuResult.success().put("data",list);
    }

    /**
     * 查询角色信息接口
     */

    @GetMapping("/byOne")
    public ShuiGuResult getRoleById(Long roleId){
        return ShuiGuResult.success(umsRoleService.getById(roleId));
    }

    /**
     * 角色分页查询接口
     */
    @GetMapping("/list/page")
    public ShuiGuResult list(UmsRoleParamDto umsRoleParamDto){
        return ShuiGuResult.success(umsRoleService.listByPage(umsRoleParamDto));
    }

    /**
     * 角色删除接口
     * @param ids
     * @return
     */

    @DeleteMapping
    public ShuiGuResult deleteRoleById(@RequestParam Long[] ids){
        boolean flag = umsRoleService.removeBatchByIds(Arrays.asList(ids));
        if(flag) {
            return ShuiGuResult.success("删除成功");
        }
        return ShuiGuResult.error("删除失败");
    }

    /**
     * 角色更新接口
     * @param role
     * @return
     */

    @PutMapping
    public ShuiGuResult updateRoleById(@RequestBody UmsRole role){
        boolean flag = umsRoleService.updateById(role);
        if(flag) {
            return ShuiGuResult.success("更新成功");
        }
        return ShuiGuResult.error("更新失败");
    }
}
